#ifndef CONNECTION_H
#define CONNECTION_H

#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>


static bool createConnection(QString strHostname, QString strDatabase, QString strUser, QString strPasswd)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(strHostname);
    QString dsn = QString("Driver={sql server};server=%1;database=%2;uid=%3;pwd=%4;")
            .arg(strHostname).arg(strDatabase)
            .arg(strUser).arg(strPasswd);

    db.setDatabaseName(dsn);
    db.setUserName(strUser);
    db.setPassword(strPasswd);


    if (!db.open()) {
        QMessageBox::critical(0, qApp->tr("Cannot open database"), db.lastError().text(), QMessageBox::Cancel);
        return false;
    }

    QSqlQuery query;
    query.exec("create table person (id int primary key, "
               "firstname varchar(20), lastname varchar(20))");

    return true;
}

#endif
